package com.jushu.storbox.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.entities.DownloadObject;
import com.jushu.storbox.util.LogUtil;

public class DownloadFileDao extends BaseDao  implements IDownloadFileDao {
	private final static String LOG_TAG = "DownloadFileDao";
	
	@Override
	public long getLastUpdateTime(String id) {
		long updateTime = 0;
		String sql = "select * from " + IDownloadFileDao.TABLE_NAME +" where "+IBaseDao.id+"=? and "+IBaseDao.owner_by+"=?";
		String[] selectionArgs = new String[]{id, ShareDriveApplication.getInstance().getWnerID()};
		List<DownloadObject> resList = queryForResult(sql,selectionArgs);
		if(resList != null && !resList.isEmpty()) {
			DownloadObject obj = resList.get(0);
			updateTime = obj.getLastUpdateTime();
		}
		return updateTime;
	}
	
	@Override
	public void updateLastUpdateTime(String objectId) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public long insertFile(DownloadObject fileInfo) {
		long row = -1;
		try {
			DownloadObject fileInfoResponse = getDownloadFileById(fileInfo.getId(), fileInfo.getOwnerBy());
			if(null != fileInfoResponse){
				LogUtil.e(LOG_TAG , "fileDao no need insertFile, id identical");
				return row;
			}
			
			ContentValues cv = fileInfo.toContentValues();
			row = this.getDb().insert(IDownloadFileDao.TABLE_NAME, null, cv);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG, "insertFile fail:" + e.getLocalizedMessage());
		} 
		return row;
	}
	
	@Override
	public int updateDownloadObject(DownloadObject fileInfo) {
		int row = -1;
		try {
			if(fileInfo == null) {
				return row;
			}
			
			String whereClause = id + "=? and " 
				+ IBaseDao.owner_by + "=? and " 
				  + current_user_id + "=?";
			
			String[] whereArgs = new String[] {
				fileInfo.getId(), 
				fileInfo.getOwnerBy(),
				ShareDriveApplication.getInstance().getWnerID()
			};
			
			row = this.getDb().update(IDownloadFileDao.TABLE_NAME,
					fileInfo.toContentValues(),
					whereClause, 
					whereArgs);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG, "insertFile fail:" + e.getLocalizedMessage());
		} 
		return row;
	}
	
	@Override
	public void deleteFile(String id, String ownerId) {
		try {
			if (null != id) {
				this.getDb().execSQL("delete from " + IDownloadFileDao.TABLE_NAME + " where id = ? and "
							+IBaseDao.owner_by+"=?",
						new String[] { id, ownerId });
			}
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG, "deleteFile is fail:" + e.getLocalizedMessage());
		}
	}

	@Override
	public void updateDownloadState(String id, String ownerId, int transStatus) {
		try {
			if(null != id){
				this.getDb().execSQL("update " + IDownloadFileDao.TABLE_NAME + " set " + IBaseDao.trans_status + " = ? where id = ? and "+IBaseDao.owner_by+"=?",
						new String[] { ""+transStatus, id, ownerId});
			}	
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG, "update transStatus is fail:" + e.getLocalizedMessage());
		}
	}
	
	public void updateProgress(String id, String ownerId, long currentSize, long currentProgress) {
		try {
			if(null != id){
				String whereClause = IBaseDao.id+"=? and "+IBaseDao.owner_by+"=?";
				String[] whereArgs = new String[]{ id, ownerId};
				ContentValues cv = new ContentValues();
				cv.put(IBaseDao.trans_status, Constant.TRANSLATE_IS_RUNNING);
				cv.put(IBaseDao.current_size,currentSize);
				cv.put(DBHelper.current_progress,currentProgress);
				getDb().update(IDownloadFileDao.TABLE_NAME, cv, whereClause, whereArgs);
			}	
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(LOG_TAG, "update transStatus is fail:" + e.getLocalizedMessage());
		}
	}
	
	@Override
	public List<DownloadObject> getDownloadFileList() {
		String sql = "select * from " + IDownloadFileDao.TABLE_NAME 
				+" where "+current_user_id+"=?";
		String[] selectionArgs = new String[]{ShareDriveApplication.getInstance().getWnerID()};
		return queryForResult(sql,selectionArgs);
	}
	
	@Override
	public List<DownloadObject> getComplateDownloadFileList() {
		String sql = "select * from " + IDownloadFileDao.TABLE_NAME 
				+" where "
				+current_user_id+"=? and "+IBaseDao.trans_status +"=?";
		String[] selectionArgs = new String[]{ShareDriveApplication.getInstance().getWnerID(),""+Constant.TRANSLATE_IS_SUCCESS}; 
		return queryForResult(sql,selectionArgs);
	}
	
	@Override
	public List<DownloadObject> getNotComplateDownloadFileList() {
		String sql = "select * from " + IDownloadFileDao.TABLE_NAME 
				+" where "
				+current_user_id+"=? and "+IBaseDao.trans_status +"<>?";
		String[] selectionArgs = new String[]{ShareDriveApplication.getInstance().getWnerID(),""+Constant.TRANSLATE_IS_SUCCESS}; 
		return queryForResult(sql,selectionArgs);
	}

	private List<DownloadObject> queryForResult(String sql, String[] selectionArgs) {
		List<DownloadObject> fileInfoList = new ArrayList<DownloadObject>();
		Cursor cursor = null;
		try {
			cursor = this.getDb().rawQuery(sql, selectionArgs);
			while(cursor.moveToNext()) {
				DownloadObject fileInfo = new DownloadObject();
				fileInfo.fromCursor(cursor);
				fileInfoList.add(fileInfo);
			}
		} catch (Exception e) {
			LogUtil.e(LOG_TAG, "getFileList fail:" + e.getLocalizedMessage());
		} finally {
			if(cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		cursor = null;
		return fileInfoList;
	}

	@Override
	public DownloadObject getDownloadFileById(String id, String ownerId) {
		DownloadObject downloadObject = null;
		String sql = "select * from " + IDownloadFileDao.TABLE_NAME + " where "+ IBaseDao.id +" = ? and "+IBaseDao.owner_by+"=?";
		String[] selectionArgs = new String[]{id, ownerId};
		List<DownloadObject> resList = queryForResult(sql,selectionArgs);
		if(resList != null && !resList.isEmpty()) {
			downloadObject = resList.get(0);
		}
		return downloadObject;
	}

}
